package com.example.wxback.push;

import com.alibaba.fastjson.JSONObject;
import com.example.wxback.model.PushResultInfo;

import java.util.ResourceBundle;

public class PushUtil {
    private static String appAndroidkey = "614e934966b59330aa70045d";
    private static String appAndroidMasterSecret = "zy7rr2iq9hlgneq8mna5kvmdhvgvcykm";

    private static String url = "http://msg.umeng.com/api/send";
    private static boolean mode = true;    //模式 true正式模式；false测试模式
    private static PushClient client = new PushClient();

    static {
        if (appAndroidkey.equals("")) {
            ResourceBundle resource = ResourceBundle.getBundle("push");
            appAndroidkey = resource.getString("appAndroidkey");
            appAndroidMasterSecret = resource.getString("appAndroidMasterSecret");
            appAndroidkey = resource.getString("appIOSkey");
            appAndroidMasterSecret = resource.getString("appIOSMasterSecret");
            url = resource.getString("url");
            mode = resource.getObject("mode") == "true" ? true : false;
        }
    }

    private static JSONObject getAndroidParamData(String deviceToken) {
        JSONObject paramMap = new JSONObject();
        paramMap.put("appkey", appAndroidkey);
        String timestamp = Integer.toString((int) (System.currentTimeMillis() / 1000));
        paramMap.put("timestamp", timestamp);

        paramMap.put("type", "unicast");
        paramMap.put("device_tokens", deviceToken);

        JSONObject payload = new JSONObject();

        JSONObject body = new JSONObject();
        body.put("title", "");
        body.put("text", "");
        body.put("ticker", "");
        body.put("custom", deviceToken);
        body.put("after_open", "go_custom");

        payload.put("body", body);
        paramMap.put("payload", payload);
        paramMap.put("production_mode", mode);
        payload.put("display_type", "message");

        return paramMap;
    }

    /**
     * 发送安卓别名推送通知
     *
     * @throws Exception
     */
    public static PushResultInfo sendAndroidCustomizedcast(String deviceToken) throws Exception {
        JSONObject paramMap = getAndroidParamData(deviceToken);
        if (paramMap == null) {
            PushResultInfo pushResultInfo = new PushResultInfo();
            pushResultInfo.setSuccess(false);
            pushResultInfo.setContent("构建推送消息失败");
            return pushResultInfo;
        }
        return client.send(url, paramMap.toJSONString(), appAndroidMasterSecret);
    }

    public static void main(String[] args) {
        try {
            sendAndroidCustomizedcast("10003");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}